//
//  YXJCDetailVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "AQJCDetailVC.h"
#import "TYBaseTableViewCell.h"
#import "YXJCDetailEChartsView.h"
#import "AQJCDetailCell1.h"
#import "StepBarController.h"
#import "ZGSHVC.h"
NSDictionary* StatusMap = @{
    @"1":@"待整改",
    @"2":@"已派发",
    @"3":@"处理中",
    @"4":@"已完成",
    @"9":@"已关闭"
};
@interface AQJCDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView * lineImgView;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, copy) NSArray * dataArr;
@property (nonatomic, strong) TYBoxView * bottomBgView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) YXJCDetailEChartsView *chartView;

@property (nonatomic, assign) NSInteger  currentTag;
@property (nonatomic, strong) UILabel * tipLab;
@property (nonatomic, strong) UILabel * tipContentLab;
@property (nonatomic, strong) AQJCModel * detailModel;

@property (nonatomic, strong) StepBarController * step;
@end

@implementation AQJCDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"安全检查详情";
 
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    kWEAK_SELF;
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%@",TYGetSecurityDetail,self.ID]  parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            weakSelf.detailModel = [AQJCModel yy_modelWithJSON:data[@"data"]];
            NSArray *reversedArray = [[weakSelf.detailModel.progressMirrorDtos reverseObjectEnumerator] allObjects];
            NSMutableArray * mreArr = reversedArray.mutableCopy;
            [mreArr sortUsingComparator:^NSComparisonResult(ProgressMirror *date1, ProgressMirror *date2) {
        
                return [date2.czsj compare:date1.czsj]; // 倒序排列
            }];
            self.dataArr = mreArr;
            [weakSelf initUI];
        }
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}
- (void)initUI {
    for (UIView * view in self.view.subviews) {
        [view removeFromSuperview];
    }
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, self.view.height)];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    TYBoxView *tipBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:tipBgView];
    [tipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(12);
        make.right.offset(-12);
        make.width.equalTo(scrollView.mas_width).offset(-24);
        make.height.offset(40);
    }];
    
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    tipLab.text = StatusMap[self.detailModel.clzt];
    NSInteger clzt = [self.detailModel.clzt integerValue];
    switch (clzt) {
        case 1:
            tipLab.textColor = hexColor(cc4cd4);
            break;
        case 2:
            tipLab.textColor = hexColor(f3a642);
            break;
        case 3:
            tipLab.textColor = hexColor(32afff);
            break;
        case 4:
            tipLab.textColor = hexColor(21b91d);
            break;
        case 9:
            tipLab.textColor = hexColor(606266);
            break;
        default:
            break;
    }
    [tipBgView addSubview:tipLab];
   
    self.tipLab = tipLab;
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
    }];
    
    if(clzt == 4){
        UIButton *startBtn = [UIButton buttonWithTitle:@"整改审核" titleColor:main_color font:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium] target:self action:@selector(onStartClick)];
        startBtn.borderColor = main_color;
        startBtn.borderWidth = 0.5;
        startBtn.cornerRadius = 2;
     
        [tipBgView addSubview:startBtn];
        [startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.centerY.offset(0);
            make.width.offset(48);
            make.height.offset(16);
        }];
    }
    UILabel *tipContentLab = [[UILabel alloc] init];
    tipContentLab.font = [UIFont systemFontOfSize:14];
    tipContentLab.textColor = main_light_text_color_6;
    tipContentLab.text = [NSString stringWithFormat:@"整改人员需在%@前完成",self.detailModel.jzsj];
    [tipBgView addSubview:tipContentLab];
    self.tipContentLab = tipContentLab;
    [tipContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tipLab.mas_right).offset(10);
        make.centerY.offset(0);
        make.right.offset(-58);
    }];
    
    
    UIView *topBgView = [[UIView alloc] init];
    topBgView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:topBgView];
    topBgView.layer.shadowColor = [UIColor colorWithRed:156/255.0 green:175/255.0 blue:210/255.0 alpha:0.2].CGColor;
    topBgView.layer.shadowOffset = CGSizeMake(0,4);
    topBgView.layer.shadowOpacity = 1;
    topBgView.layer.shadowRadius = 8;
    topBgView.layer.cornerRadius = 7;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"基本信息";
        [topBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        ;
        NSArray *arr = @[@"整改编号",@"检查地点", @"检查分类",@"检查结果",@"养护单位",@"情况描述",@"检查日期",@"截止日期"];
        NSArray *valueArr = @[self.detailModel.bh,self.detailModel.proName, self.detailModel.jcfl, @[@"正常",@"异常",@"异常且严重"][[self.detailModel.jcjg intValue]],self.detailModel.yhDeptName,self.detailModel.wtms,self.detailModel.jcsj,self.detailModel.jzsj];
        UIStackView *currentStackView = nil;
        for (int i = 0; i<arr.count; i++) {
            UIStackView *stackView1 = [[UIStackView alloc] init];
            stackView1.spacing = 10;
            [topBgView addSubview:stackView1];
            [stackView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(15);
                if(i == 0){
                    make.top.equalTo(titleLabel.mas_bottom).offset(5);
                }else {
                    make.top.equalTo(currentStackView.mas_bottom);
                }
                make.height.offset(20);
            }];
            
            UILabel *leftLab1 = [[UILabel alloc] init];
            leftLab1.text = arr[i];
            leftLab1.textColor = main_light_text_color_9;
            leftLab1.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
            
            UILabel * ssxtLab = [[UILabel alloc] init];
            ssxtLab.text = valueArr[i];
            ssxtLab.tag = i+1000;
            ssxtLab.textColor = main_light_text_color_9;
            ssxtLab.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
            
            [stackView1 addArrangedSubview:leftLab1];
            [stackView1 addArrangedSubview:ssxtLab];
            
            currentStackView = stackView1;
        }
        
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipBgView.mas_bottom).offset(15);
            make.left.offset(12);
            make.right.offset(-12);
            make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
            make.width.equalTo(scrollView.mas_width).offset(-24);
        }];
        
    }
    
    TYBoxView *bottomBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:bottomBgView];
    self.bottomBgView = bottomBgView;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"过程跟踪";
        [bottomBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        
        StepBarController *step = [[StepBarController alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH-24, 60)];
        NSInteger index =  [self.detailModel.clzt integerValue];
        if(index == 9){
            index = 4;
        }else{
            index--;
        }
        [step setProgressStatus:@[@"待整改",@"已派发",@"处理中",@"已完成",@"已关闭"] currentStep:index];
        [bottomBgView addSubview:step];
        self.step = step;
        [step mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(10);
            make.left.right.offset(0);
            make.height.offset(60);
        }];
        
        
        [bottomBgView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(step.mas_bottom).offset(10);
            make.left.right.offset(0);
            make.height.offset(self.dataArr.count *160);
            make.bottom.offset(0);
        }];
    }

    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView.mas_bottom).offset(15);
        make.left.right.equalTo(topBgView);
        make.bottom.equalTo(scrollView.mas_bottom);
        make.width.equalTo(scrollView.mas_width).offset(-24);
    }];
    
    [scrollView layoutIfNeeded];
    
    // 模仿接口请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];

        [self.tableView performBatchUpdates:nil completion:^(BOOL finished) {
            CGFloat tableViewHeight = self.tableView.contentSize.height;
            // 在这里使用 tableViewHeight 更新其他 UI
            
            [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.step.mas_bottom).offset(10);
                make.left.right.offset(0);
                make.height.offset(tableViewHeight);
                make.bottom.offset(0);
            }];
            
            [scrollView layoutIfNeeded];
        }];
        
        
    });
}

/// MARK: - 开始整改
- (void)onStartClick {
    ZGSHVC *vc = [[ZGSHVC alloc] init];
    vc.ID = self.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AQJCDetailCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [AQJCDetailCell1 nibCell];
    }
    [cell cellWithData:self.dataArr andIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
@end


